package com.ITArmy.util;


import com.ITArmy.beans.User;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateChange {
    //对时间进行相加减
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//格式化对象
    Calendar calendar = Calendar.getInstance();//日历对象
    private static Logger logger = Logger.getLogger(DateChange.class);

    public Date addVipTime(Date lastDate, int vipTime) throws ParseException {
        calendar.setTime(lastDate);//设置当前日期
        calendar.add(Calendar.MONTH, vipTime);//月份加X
        System.out.println(sdf.format(calendar.getTime()));//输出格式化的日期
        System.out.println(sdf.parse(sdf.format(calendar.getTime())));
        return sdf.parse(sdf.format(calendar.getTime()));
    }

    //进行判断Vip是否到期
    public User CheckVipTime(User user) {
        calendar.setTime(new Date());//现在的时间
        String nowTime = new String(sdf.format(calendar.getTime()));//格式化后的日期
        try {
            if (user.getuExpire() == null) {
                return user;
            }
        } catch (Exception e) {
            return user;
        }
        calendar.setTime(user.getuExpire());//到期的时间
        String endTime = new String(sdf.format(calendar.getTime()));
        try {
            Date now = sdf.parse(nowTime);
            Date end = sdf.parse(endTime);
            if (end.before(now)) {
                user.setuVip("0");
                return user;
            } else {
                return user;
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            return user;
        }
    }

}
